﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public class PagePading
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageSize">一页有几条数据</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="totalCount">共有多少行</param>
        /// <returns></returns>
        public static string ShowPageNavigate(int pageSize, int currentPage, int totalCount)
        {
            string redirectTo = "";
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var output = new StringBuilder();
            if (totalPages > 1)
            {
                if (currentPage != 1)
                {//处理首页连接
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
                }
                else
                {
                    // output.Append("<span class='pageLink'>上一页</span>");
                }

                output.Append(" ");
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {//一共最多显示10个页码，前面5个，后面5个
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {
                        if (currint == i)
                        {//当前页处理
                            //output.Append(string.Format("[{0}]", currentPage));
                            output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
                        }
                        else
                        {//一般页处理
                            output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
                        }
                    }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                {//处理下一页的链接
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
                }
                else
                {
                    //output.Append("<span class='pageLink'>下一页</span>");
                }
                output.Append(" ");
                if (currentPage != totalPages)
                {
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
                }
                output.Append(" ");
            }
            output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
            return output.ToString();
        }
        
        #region 企汇网 新闻详细页分页
        public static readonly string StrSYHPaperBreak = "<%--#SYHpagerbreak#--%>";//企汇网 新闻详细页分页标记符
        private const string PaperBreakHtml = "<div contenteditable=\"false\" class=\"cke_pagebreak\" data-cke-display-name=\"pagebreak\" aria-label=\"Page Break\" title=\"Page Break\" style=\"page-break-after: always;\"></div>";

        /// <summary>
        /// SYH分页标记 转换html分页符 后台用
        /// </summary>
        /// <param name="strContents"></param>
        /// <returns></returns>
        public static string SYHPaperBreakToHtml(string strContents)
        {
            return strContents.Replace(StrSYHPaperBreak, PaperBreakHtml);
        }
        /// <summary>
        /// html分页符 转换SYH分页标记 后台用
        /// </summary>
        /// <param name="strContents"></param>
        /// <returns></returns>
        public static string SYHPaperHtmlToBreak(string strContents)
        {
            string regexstr = @"<div[^>]*page-break-after[^>]*\>([\w\W]*?)\</div\>";
            return Regex.Replace(strContents, regexstr, StrSYHPaperBreak, RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 输出前台页面分页连接[前台使用]动态加载css和脚本
        /// </summary>
        /// <param name="PageNameUrl">页面名称,如果使用了重写则页码为*替代，如:Machinery-7.html,此处写Machinery-*</param>
        /// <param name="Namemime">页面扩展名，如.aspx，.html,.shtml注意加.</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="totalCount">数据总数</param>
        /// <param name="TipMode">0无1页面总数2当前页3页面总数+当前页</param>
        /// <param name="UrlValue">问号后面的其他参数。普通模式如：a=1&b=2必须以&开头，因为page占了第一个参数；重写模式：第一个字符是否为&都可行</param>
        /// <param name="EndSearch">开启后，如果大于6页则输出页面的搜索</param>
        /// <param name="ServerUrl">当前路径(跟目录为Script)</param>
        /// <param name="CssName">Red/Yello/Blue</param>
        /// <param name="UrlRewriterRule">是否开启重写</param>
        /// <returns></returns>
        public static string GetPager_Links(string PageNameUrl, string Namemime, int pageIndex, int pageSize, int totalCount, int TipMode, string UrlValue, bool EndSearch, string ServerUrl, string CssName, bool UrlRewriterRule)
        {
            #region
            int totalPages = GetMaxPager(totalCount, pageSize);//获取页面总数
            if (totalPages <= 1) { return string.Empty; }//总数不足 第一页显示数 输出空 08.09 lzy修改 08.25DZT修改
            string PagerTip = GetPagerTip(totalCount, pageIndex, totalPages, TipMode);//获取分页前提示信息
            StringBuilder sb = new StringBuilder();
            #region 加载样式
            sb.Append("<link href=\"" + ServerUrl + "/style/pager/SYH.urlpager-" + CssName + ".css\" rel=\"stylesheet\" type=\"text/css\" />" + Environment.NewLine);
            #endregion
            sb.Append(PagerTip + Environment.NewLine);

            int start = 1; int end = totalPages;
            if (pageIndex > 1)
            {
                sb.Append("<span class='down'><a href=\"" + HrefLink(PageNameUrl, Namemime, pageIndex - 1, UrlValue, UrlRewriterRule) + "\">上一页</a></span>");
            }
            if (pageIndex >= 5)
            {
                sb.Append("<span class='left'><a href=\"" + HrefLink(PageNameUrl, Namemime, 1, UrlValue, UrlRewriterRule) + "\">1</a></span>");
                sb.Append("<span class='left'>... </span>");
                if (pageIndex > 2) { start = pageIndex - 2; } //当前页前面显示2个连接
                if (pageIndex <= totalPages - 5) { if (totalPages > pageIndex + 2) { end = pageIndex + 2; } } //当前页后面显示2个连接
                else { if (totalPages > pageIndex + 4) { end = pageIndex + 4; } } //当前页后面显示4个连接
            }
            else
            {
                if (pageIndex > 3) { start = pageIndex - 3; } //当前页前面显示3个连接
                if (totalPages > (pageIndex + 4)) { end = pageIndex + 4; } //当前页后面显示4个连接
            }
            //循环显示页码连接，当前页不加连接
            for (var i = start; i <= end; i++)
            {
                if (pageIndex != i)
                {
                    sb.Append("<span class='left'><a href=\"" + HrefLink(PageNameUrl, Namemime, i, UrlValue, UrlRewriterRule) + "\">" + i + "</a></span>");
                }
                else
                {
                    sb.AppendFormat("<span class='this-page left'>{0}</span>", i);
                }
            }
            //如果当前页小于总页大于等于10的话，显示最后一页
            if (pageIndex <= (totalPages - 5))
            {
                sb.Append("<span class='left'>... </span>");
                sb.Append("<span class='left'><a href=\"" + HrefLink(PageNameUrl, Namemime, totalPages, UrlValue, UrlRewriterRule) + "\">" + totalPages + "</a></span>");
            }
            if (pageIndex < totalPages)
            {
                sb.Append("<span  class='down'><a href=\"" + HrefLink(PageNameUrl, Namemime, pageIndex + 1, UrlValue, UrlRewriterRule) + "\">下一页</a></span>");
            }
            //如果总页码大于6并且启动了搜索页码就出现搜索条
            if (totalPages >= 6 && EndSearch)
            {

                string PagerEndS = GetPagerEnd(totalPages, HrefLink(PageNameUrl, Namemime, UrlValue, UrlRewriterRule), ServerUrl);
                sb.Append(PagerEndS);
            }
            return sb.ToString();
            #endregion
        }


        public static string FirstPageLink = null;
        /// <summary>
        /// 获取当前数据的最大分页数量[前台后台分页均调用，邓振涛]
        /// </summary>
        /// <param name="totalCount">数据总数</param>
        /// <param name="pageSize">每页的显示多少条数据</param>
        /// <returns></returns>
        public static int GetMaxPager(int totalCount, int pageSize)
        {
            #region
            int totalPages = totalCount / pageSize;//获取分页总数
            if (totalCount % pageSize != 0)//判断分页是否整除，否则增加一页
            {
                totalPages += 1;
            }

            if (totalPages == 1)//如果只有一页数据，分页代码输出空
                totalPages = 1;
            return totalPages;
            #endregion
        }

        /// <summary>
        /// 获取分页的开头提示[内部方法仅供GetPager_Links调用]
        /// </summary>
        /// <param name="totalCount">totaCount页面总数</param>
        /// <param name="PageIndex">PageIndex当前页面</param>
        /// <param name="totalPages">totalPages页面总数</param>
        /// <param name="TipMode">0无1页面总数2当前页3页面总数+当前页</param>
        /// <returns></returns>
        public static string GetPagerTip(int totalCount, int PageIndex, int totalPages, int TipMode)
        {
            string PagerTip = "";
            if (TipMode == 1) { PagerTip = "<span class=\"paddTop\">总记录<font color=red><b> " + totalCount + " </b></font>&nbsp;条</span>"; }
            if (TipMode == 2) { PagerTip = "<span class=\"paddTop\">当前&nbsp;<b><font color=red>" + PageIndex + "</font>&nbsp;/&nbsp;<font color=blue>" + totalPages + " </font></b>&nbsp;页</span>"; }
            if (TipMode == 3) { PagerTip = "<span class=\"paddTop\">总记录<font color=red><b> " + totalCount + " </b></font>&nbsp;条&nbsp;&nbsp;&nbsp;&nbsp;当前&nbsp;<b><font color=red>" + PageIndex + "</font>&nbsp;/&nbsp;<font color=blue>" + totalPages + " </font></b>&nbsp;页</span>"; }
            return PagerTip;
        }

        /// <summary>
        /// 输出连接
        /// </summary>
        /// <param name="PageNameUrl">页面名称,如果使用了重写则页码为*替代，如:Machinery-7.html,此处写Machinery-*</param>
        /// <param name="Namemime">页面扩展名，如.aspx，.html,.shtml注意加.</param>
        /// <param name="nowPageIndex">当前页码</param>
        /// <param name="UrlValue">问号后面的其他参数。普通模式如：a=1&b=2必须以&开头，因为page占了第一个参数；重写模式：第一个字符是否为&都可行</param>
        /// <param name="UrlRewriterRule">是否开启重写</param>
        /// <returns></returns>
        public static string HrefLink(string PageNameUrl, string Namemime, int nowPageIndex, string UrlValue, bool UrlRewriterRule)
        {
            #region
            if (string.IsNullOrEmpty(UrlValue)) { UrlValue = ""; }//如果为null则替换为空
            string PageUrl = PageNameUrl + Namemime + "?page=" + nowPageIndex + "" + UrlValue;
            if (UrlRewriterRule)
            {
                //验证是否替换首页，设置了FristPageLink的值，并且当前页为1时，直接输出设置的连接地址
                if (!string.IsNullOrEmpty(FirstPageLink) && nowPageIndex == 1)
                {
                    PageUrl = FirstPageLink;
                }
                else
                {
                    PageNameUrl = PageNameUrl.Replace("*", nowPageIndex.ToString());//替换*号为当前页
                    //判断后尾参数是否为空并且验证第一个参数是否习惯性用了&
                    if (UrlValue != "")
                    {
                        string FirstUrlFolt = UrlValue.Substring(0, 1);
                        if (FirstUrlFolt != "?" && FirstUrlFolt != "&")
                        {
                            UrlValue = "?" + UrlValue;//如果参数没有以任何形式开头，则自动加入？号
                        }
                        else
                        {
                            if (FirstUrlFolt == "&") { UrlValue = "?" + UrlValue.Substring(1, UrlValue.Length - 1); }//如果以&开头则替换为?号
                        }
                    }
                    PageUrl = PageNameUrl + Namemime + UrlValue;
                }
            }
            return PageUrl;
            #endregion
        }

        /// <summary>
        /// 输出连接[输出一个完整的连接，但是page参数是*，需要替换页面数字]
        /// </summary>
        /// <param name="PageNameUrl">页面名称,如果使用了重写则页码为*替代，如:Machinery-7.html,此处写Machinery-*</param>
        /// <param name="Namemime">页面扩展名，如.aspx，.html,.shtml注意加.</param>
        /// <param name="nowPageIndex">当前页码</param>
        /// <param name="UrlValue">问号后面的其他参数。普通模式如：a=1&b=2必须以&开头，因为page占了第一个参数；重写模式：第一个字符是否为&都可行</param>
        /// <param name="UrlRewriterRule">是否开启重写</param>
        /// <returns></returns>
        public static string HrefLink(string PageNameUrl, string Namemime, string UrlValue, bool UrlRewriterRule)
        {
            #region
            if (string.IsNullOrEmpty(UrlValue)) { UrlValue = ""; }//如果为null则替换为空
            string PageUrl = PageNameUrl + Namemime + "?page=*" + UrlValue;
            if (UrlRewriterRule)
            {
                //判断后尾参数是否为空并且验证第一个参数是否习惯性用了&
                if (UrlValue != "")
                {
                    string FirstUrlFolt = UrlValue.Substring(0, 1);
                    if (FirstUrlFolt != "?" && FirstUrlFolt != "&")
                    {
                        UrlValue = "?" + UrlValue;//如果参数没有以任何形式开头，则自动加入？号
                    }
                    else
                    {
                        if (FirstUrlFolt == "&") { UrlValue = "?" + UrlValue.Substring(1, UrlValue.Length - 1); }//如果以&开头则替换为?号
                    }
                }
                PageUrl = PageNameUrl + Namemime + UrlValue;
            }
            return PageUrl;
            #endregion
        }

        /// <summary>
        /// 输出页面的搜索和跳转(javascript:return CheckPage调用js的验证方法)[内部方法仅供GetPager_Links调用]
        /// </summary>
        /// <param name="totalPages">页码总数</param>
        /// <param name="PageNameUrl">调用HrefLink(string PageNameUrl, string Namemime,string UrlValue, bool UrlRewriterRule)在js中进行page替换</param>
        /// <param name="ServerUrl">当前路径(跟目录为Script)</param>
        /// <returns></returns>
        public static string GetPagerEnd(int totalPages, string PageNameUrl, string ServerUrl)
        {
            int checkfirstlink = 0; string urlfirstlink = FirstPageLink == null ? "#" : "" + FirstPageLink + "";
            if (!string.IsNullOrEmpty(FirstPageLink)) { checkfirstlink = 1; }
            string FunCheckPage = "'" + PageNameUrl + "'," + checkfirstlink + ",'" + urlfirstlink + "'";
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"pageTitle\">到第<input class=\"inputLine\" id=\"txtInPage\" style=\"WIDTH: 30px\" size=4 autocomplete=\"off\">页</div>");
            sb.Append("<span class=\"left\"><a class=\"view\" id=\"btnCFTPage\" onclick=\"javascript:return CheckPage(" + FunCheckPage + ");\" href=\"#\">查看</a></span><input id=\"txt_totalPages\" value=\"" + totalPages + "\" style=\"display:none\"  />" + Environment.NewLine);
            sb.Append("<script language='javascript' src=\"/" + ServerUrl + "/SYH.PagerCheck.js\" charset=\"UTF-8\"></script>" + Environment.NewLine);
            return sb.ToString();
        }
        #endregion
    }
}
